#!/bin/bash

# Directory containing the JSON files
directory="."

# Templated command (use single quotes to prevent premature variable expansion)
templated_command='gpulab-cli --cert ~/gpulab.pem submit < %q'

# Loop through all .json files in the specified directory
find "$directory" -maxdepth 1 -name "*.json" -print0 | while IFS= read -r -d $'\0' json_file; do

  # Extract the filename without the path (optional, but often cleaner)
  filename=$(basename "$json_file")

  # Fill the filename into the templated command
  command=$(printf "$templated_command" "$json_file") # Or "$filename" if you only want the filename

  # Print the command before executing (for debugging)
  echo "Executing: $command"

  # Execute the command
  eval "$command"  # Use with caution! See explanation below.

  # Check the exit status of the command (optional)
  if [[ $? -ne 0 ]]; then
    echo "Error executing command for $filename. Exiting."
    exit 1
  fi

done

echo "Finished processing all JSON files."
